package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fabrica.dto.SolicitudFabricacion;
import fabrica.service.*;

/**
 * Servlet implementation class ServletFabrica
 */
@WebServlet("/ServletFabrica")
public class ServletFabrica extends HttpServlet {
	private static final long serialVersionUID = 1L; 
	
	@EJB
	private IFabricaService iFabrica;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletFabrica() {
        super();
        // TODO Auto-generated constructor stub
    }

    //doGet
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);	
	}

	//doPost
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String jspPage = "/index.jsp";
        String action = request.getParameter("action");
          
        if("listaSF".equals(action)){
			jspPage="listaSF.jsp";
            List<SolicitudFabricacion> lista = iFabrica.listarSolicitudesFabricacionPendintes();
            request.setAttribute("listaSol", lista);
		}
        
        if("fabricar".equals(action)){
			jspPage="index.jsp";
			String[] elegidos = request.getParameterValues("checkList");
			
			if(elegidos != null)
        	{
				List<String> elegidosList = new ArrayList<String>();
				for(String s : elegidos)
					elegidosList.add(s);
				iFabrica.confirmarSolicitudesFabricacion(elegidosList);
        	}	
		}
        
        
 
        
        dispatch(jspPage, request, response);
	}
	
	
	//dispatch
    protected void dispatch(String jsp, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        if (jsp != null){
            RequestDispatcher rd = request.getRequestDispatcher(jsp);
            rd.forward(request, response);
        }
    }

}
